package service4admin

import (
	"Gin-Gorm-boke/boke_admin/models4admin"
	helper "Gin-Gorm-boke/boke_common/helper"
	models "Gin-Gorm-boke/boke_common/models"
	"Gin-Gorm-boke/boke_common/redis"
	"Gin-Gorm-boke/boke_common/response"
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
	"time"
)

type UserDto struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

type AdminUserInfoVo struct {
	gorm.Model
	User        UserInfoVo `json:"user"`
	Roles       []string   `json:"roles"`
	Permissions []string   `gorm:"column:perms" json:"permissions"`
}

type UserInfoVo struct {
	gorm.Model
	Id       int    `gorm:"column:id;" json:"id"`
	NickName string `gorm:"column:nick_name;" json:"nickName"`
	Sex      string `gorm:"column:sex;" json:"sex"`
	Email    string `gorm:"column:email;" json:"email"`
	Avatar   string `gorm:"column:avatar;" json:"avatar"`
}

func Login(c *gin.Context) {
	var userDto UserDto
	if err := c.ShouldBindJSON(&userDto); err != nil {
		response.ResponseBadRequest(c)
		return
	}
	var user models.User
	err := models.Db.Model(new(models.User)).Where("user_name=?", userDto.Username).Where("type = ?", "1").First(&user).Error
	fmt.Println(user)
	if err != nil {
		response.RespnseLoginError(c)
		return
	}
	if !helper.AnalysisBcryptPassword(user.Password, userDto.Password) {
		response.RespnseLoginError(c)
		return
	}
	token, _ := helper.GenerateToken(user.Id)

	redis.SetUserInfo("bloglogin:"+strconv.Itoa(user.Id), user, time.Hour*24)

	if err != nil {
		fmt.Println(err)
	}

	response.ResponseOK(c, gin.H{
		"token": token,
	})
	return

}

func GetInfo(c *gin.Context) {
	userId, exists := c.Get("userId")
	if !exists {
		response.RespnseLoginError(c)
		return
	}
	var userInfo AdminUserInfoVo
	var count int64
	models.Db.Model(new(models4admin.UserRole)).
		Table("sys_user_role ur").
		Joins("join sys_role r on ur.role_id=r.id").
		Where("ur.user_id=?", userId).
		Where("r.role_key = ?", "admin").
		Count(&count)
	if count > 0 {
		models.Db.Model(new(models4admin.Menu)).
			Select("perms").
			Where("menu_type in ?", []string{"C", "F"}).
			Where("status = ?", "0").
			Find(&userInfo.Permissions)
		userInfo.Roles = append(userInfo.Roles, "admin")
	} else {
		models.Db.Model(new(models4admin.UserRole)).
			Table("sys_user_role ur").
			Select("m.perms").
			Joins("join sys_role_menu rm on ur.role_id=rm.role_id").
			Joins("join sys_menu m on rm.menu_id=m.id").
			Where("ur.user_id=?", userId).
			Where("m.menu_type in ?", []string{"C", "F"}).
			Where("m.status = ?", "0").
			Find(&userInfo.Permissions)
		models.Db.Model(new(models4admin.Role)).
			Table("sys_role r").
			Select("r.role_key").
			Joins("join sys_user_role ur on r.id=ur.role_id").
			Where("ur.user_id=?", userId).
			Where("r.status = ?", "0").
			Find(&userInfo.Roles)
	}
	models.Db.Model(new(models.User)).
		Where("id=?", userId).
		Find(&userInfo.User)
	response.ResponseOK(c, userInfo)
	return
}

func Logout(c *gin.Context) {
	token := c.GetHeader("token")
	userClaim, _ := helper.AnalysisToken(token)
	userId := userClaim.UserId
	models.RDb.Del(context.Background(), "bloglogin:"+strconv.Itoa(userId))
	response.ResponseOK(c, nil)

}
